package com.neusoft.servelt;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;
import java.util.Enumeration;

/**
 * @Author: duan
 * @Date: Create in 10:37 2020/10/17
 */
@WebServlet(name = "demoServlet", urlPatterns = "/demo", initParams = {
        @WebInitParam(name = "myname", value = "haha"),
        @WebInitParam(name = "qita", value = "buzhid")
})
public class DemoServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doPost");
        System.out.println("测试热部署");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doGet");
        System.out.println("测试热部署");
        ServletConfig config = getServletConfig();
        System.out.println(config.getServletName());
        Enumeration<String> initParameterNames = config.getInitParameterNames();
        while (initParameterNames.hasMoreElements()) {
            String name = initParameterNames.nextElement();
            System.out.println(name);
            System.out.println(config.getInitParameter(name));
        }

        ServletContext context = getServletContext();//域对象
        System.out.println(context.getRealPath("/"));//获取/表示的绝对路径
        context.setAttribute("name", "haha");
        System.out.println(context.getContextPath());
        System.out.println(context.getServerInfo());
        initParameterNames = context.getInitParameterNames();
        System.out.println(initParameterNames.hasMoreElements());
        while (initParameterNames.hasMoreElements()) {
            String name = initParameterNames.nextElement();
            System.out.println(name);
            System.out.println(context.getInitParameter(name));
        }
        System.out.println(context.getServletContextName());
//        context.addServlet("/dynamic", DynamicServlet.class);
        System.out.println(context.getServletRegistrations());
        Collection<String> jsp = context.getServletRegistration("demoServlet").getMappings();//demoServlet的映射路径
        jsp.stream().forEach(item -> System.out.println(item));
        System.out.println(context.getVirtualServerName());
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
        /*
            在service方法，做了判断
                如果是get请求，那么就由doGet方法来处理
                如果是dopost请求，那么就是由doPost方法来处理
                该逻辑是在HttpServlet中实现的service方法实现的
         */
        System.out.println("service");
    }
}
